import requests as re
import parsel
import csv
import time

with open("Cookie.txt", mode='r') as f:
    cookies = f.read()
movie_list = [36151692, 35426925, 35633650, 35712804, 26925611, 26715496, 36208094, 36369452, 36438166, 34841067,
              26290365, 26363254, 25845392, 35267224, 35660795]
for movie in movie_list:
    for page in range(0, 600, 20):
        url = f'https://movie.douban.com/subject/{movie}/comments?start={page}&limit=20&status=P&sort=new_score'
        headers = {
            "Cookie": cookies,
            "User-Agent":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 "
                "Safari/537.36 Edg/115.0.1901.188 "
        }
        url_html = re.get(url, headers=headers).text
        url_html.encode(encoding='utf-8')
        selector = parsel.Selector(url_html)
        comment_list1 = selector.css(".comment-item")
        for comment in comment_list1:
            name = comment.css(".comment-info a::text").get().strip()
            short = comment.css(".short::text").get().strip()
            time1 = comment.css(".comment-time::text").get().strip()
            location = comment.css(".comment-location::text").get()
            print(name, short, time1, location)
            with open("电影评论.csv", mode='a', encoding="utf-8", newline='') as f:
                writer_csv = csv.writer(f)
                writer_csv.writerow([name, short, time1, location])
        time.sleep(2)

time.sleep(2)
